#!/bin/bash
#backupAllSchoolDatabases

# delete files 30  days ago
rm -rf `find /var/nas/*/bxnlog/logs/*/ -mtime +15`
rm -rf `find /var/nas/*/db_bak -mtime +30`


pods=`kubectl get pod | grep mysql | awk '{print $1}'`
month="$(date +%Y%m)"
for pod in $pods;do
        time="$(date +%Y-%m-%d%H:%M:%S)"
        codeLower=`echo $pod | awk -F '-' '{print $1}'`
        schoolCode=`echo $codeLower | tr '[a-z]' '[A-Z]'`
        echo $schoolCode
        logpath=/var/nas/logs
        test ! -d "$logpath" && mkdir -p $logpath
        logfile=/var/nas/logs/backup-$month.log
        test ! -f "$logfile" && touch $logfile
        echo "$time begin to back $schoolCode" >> $logfile
        kubectl exec $pod /bin/bash /usr/local/data/backupAllDbs.sh
done
